package com.zhongde.cloud89app.activity;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.tu.loadingdialog.LoadingDailog;
import com.zhongde.cloud89app.MainActivity;
import com.zhongde.cloud89app.R;
import com.zhongde.cloud89app.utils.MyPreference;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

import static com.zhongde.cloud89app.base.Configuration.BASE_URL;

public class RegActivity extends AppCompatActivity {

    private Button btn_reg;
    private EditText reg_username, reg_userpwd, reg_re_userpwd, reg_user_nickname, reg_user_menuname, reg_user_menu_password;
    private MyPreference pref;
    private LoadingDailog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("用户注册");
        actionBar.setDisplayHomeAsUpEnabled(true);
        pref = new MyPreference(RegActivity.this);
        btn_reg = findViewById(R.id.btn_reg);

        LoadingDailog.Builder loadBuilder = new LoadingDailog.Builder(this)
                .setMessage("注册中...")
                .setCancelable(false)
                .setCancelOutside(false);
        dialog = loadBuilder.create();
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RegAction();
            }
        });


    }

    private void RegAction() {
        reg_username = findViewById(R.id.reg_username);
        reg_userpwd = findViewById(R.id.reg_userpwd);
        reg_re_userpwd = findViewById(R.id.reg_re_userpwd);
        reg_user_nickname = findViewById(R.id.reg_user_nickname);
        reg_user_menuname = findViewById(R.id.reg_user_menuname);
        reg_user_menu_password = findViewById(R.id.reg_user_menu_password);

        String text_username = reg_username.getText().toString().trim();
        String text_userpwd = reg_userpwd.getText().toString().trim();
        String text_re_userpwd = reg_re_userpwd.getText().toString().trim();
        String text_user_nickname = reg_user_nickname.getText().toString().trim();
        String text_user_menuname = reg_user_menuname.getText().toString().trim();
        String text_user_menu_password = reg_user_menu_password.getText().toString().trim();

        if (text_username.isEmpty() | text_userpwd.isEmpty() | text_re_userpwd.isEmpty() | text_user_nickname.isEmpty() | text_user_menuname.isEmpty() | text_user_menu_password.isEmpty()) {
            Toast.makeText(this, "请补全信息", Toast.LENGTH_SHORT).show();
            return;
        }

        dialog.show();
        OkHttpUtils
                .post()
                .url(BASE_URL + "AppcallingInterface_Controller/user_register.do")

                .addParams("username", text_username)
                .addParams("menu_password", text_user_menu_password)
                .addParams("menuname", text_user_menuname)
                .addParams("password", text_userpwd)
                .addParams("user_nickname", text_user_nickname)
                .addParams("affirm_password", text_re_userpwd)
                .build()
                .execute(new StringCallback() {
                    @Override

                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(RegActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);

                            Toast.makeText(RegActivity.this, jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
                            if (jsonObject.getInt("code") == 1) {

                                pref.putStringAndCommit("userName", reg_username.getText().toString().trim());
                                pref.putStringAndCommit("password", reg_userpwd.getText().toString().trim());
                                pref.putStringAndCommit("describe", reg_user_nickname.getText().toString().trim());

                                finish();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onAfter(int id) {
                        super.onAfter(id);
                        dialog.hide();
                    }
                });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }


}